package com.huohuzhihui.merchant.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.exception.CustomException;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.uuid.IdUtils;
import com.huohuzhihui.merchant.domain.Goods;
import com.huohuzhihui.merchant.domain.OrderDetail;
import com.huohuzhihui.merchant.mapper.OrderDetailMapper;
import com.huohuzhihui.merchant.service.IGoodsService;
import com.ijpay.core.enums.SignType;
import com.ijpay.core.enums.TradeType;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.model.UnifiedOrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.huohuzhihui.merchant.mapper.OrderMapper;
import com.huohuzhihui.merchant.domain.Order;
import com.huohuzhihui.merchant.service.IOrderService;

import javax.annotation.PostConstruct;

/**
 * 订单Service业务层处理
 *
 * @author huohuzhihui
 * @date 2021-06-09
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private IGoodsService goodsService;


    /**
     * 查询订单
     *
     * @param id 订单ID
     * @return 订单
     */
    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids) {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单ID
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id) {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public Order recharge(SysUser sysUser, String ip, int source, String payChannel, Long goodsId, String openId) {
        //查询购买商品
        Goods goods = goodsService.selectGoodsById(goodsId);

        //保存订单
        Order order = new Order();
        order.setAmount(goods.getPrice());
        order.setUserId(sysUser.getUserId());
        order.setMerchantId(sysUser.getDeptId());
        order.setSource(source);
        order.setStatus(0);
        insertOrder(order);

        //保存订单明细
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setAmount(goods.getPrice());
        orderDetail.setOrderId(order.getId());
        orderDetail.setGoodsId(goodsId);
        orderDetail.setStatus(0);
        orderDetail.setQuantity(1L);
        orderDetail.setPayChannel(payChannel);
        orderDetailMapper.insertOrderDetail(orderDetail);

        order.setGoods(this.goodsService.selectGoodsById(goodsId));
        return order;
    }
}
